

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
//import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.io.ObjectOutputStream;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
//import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
//import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.util.Arrays;
import java.util.Date;
import java.util.Random;
import java.util.Vector;


public class MainGui extends JFrame {

	private static final long serialVersionUID = -7325837415184571677L;
	public static final int WIDTH = 1100;
	public static final int HEIGHT = 600;
	Pledge pledges;
	
	double A;
	double B;
	double alpha;
	int round;
	
	Client client;
	//GamblePane[] pages;
	JPanel gamblePanel, decisionPanel, bottom;
	/* body1 was removed from JLabel */
	JLabel body2, messages;
	JButton next;
	Date now;
	long time;

	public MainGui(Pledge p, double A, double B, double alpha, Client cl) {
		super();

		// full screen
		setExtendedState(Frame.MAXIMIZED_BOTH);
		setUndecorated(true);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		// the panel that contains everything
		JPanel mainPanel = new JPanel();
		add(mainPanel);
		mainPanel.setLayout(new BorderLayout());

		this.A = A;
		this.B = B;
		this.alpha = alpha;
		
		pledges = p;
		client = cl;

		// creates header top within MainPanel
		JPanel top = new JPanel();
		top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
		JPanel header1 = new JPanel();
		JLabel header1L = new JLabel("University of California San Diego, Economics Department");
		header1.add(header1L);
		header1.setBackground(new Color(100, 149, 237));
		header1.setOpaque(true);
		JPanel header2 = new JPanel();
		JLabel header2L = new JLabel("Decision");
		header2.add(header2L);
		header2.setBackground(new Color(255, 215, 0));
		header2.setOpaque(true);
		header1.setAlignmentX(Component.CENTER_ALIGNMENT);
		header2.setAlignmentX(Component.CENTER_ALIGNMENT);
		top.add(header1);
		top.add(header2);

		// centerPanel of mainPanel
		JPanel centerPanel = new JPanel();
		centerPanel.setOpaque(true);
		centerPanel.setBackground(Color.white);
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		centerPanel.setLayout(layout);

		// within CenterPanel, DecisionPanel
		decisionPanel = new JPanel();
		// decisionPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		decisionPanel.setLayout(new BoxLayout(decisionPanel, BoxLayout.Y_AXIS));
		decisionPanel.setOpaque(true);
		decisionPanel.setBackground(Color.white);
		//body1 = new JLabel(
				//"Please, be sure to complete each decision before clicking next.");
		
		String text = "<html><center>INDEX = "+ client.data.index +"</center></html>";		
		body2 = new JLabel(text);
		
		body2.setHorizontalAlignment(JLabel.CENTER);
		//body1.setAlignmentX(Component.CENTER_ALIGNMENT);
		body2.setAlignmentX(Component.CENTER_ALIGNMENT);
		//decisionPanel.add(body1);
		decisionPanel.add(body2);

		// Panel with gambles
		gamblePanel = new JPanel();
		gamblePanel.setPreferredSize(new Dimension(WIDTH + 4, HEIGHT + 4));
		gamblePanel.setLayout(new BorderLayout());
		gamblePanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		gamblePanel.setOpaque(true);
		gamblePanel.setBackground(Color.white);

		//setup first round
		RoundPane pledgePane = new RoundPane(p, 5000, this.A, this.B, this.alpha, this.client.cinfo.roundNum);
		
		gamblePanel.add(pledgePane, BorderLayout.CENTER);
		decisionPanel.add(gamblePanel);

		// scrolls forwards
		next = new JButton("Submit");
		next.setOpaque(false);
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainGui parent = (MainGui) (((JButton) e.getSource())
						.getTopLevelAncestor());

				// record time
				Date temp = new Date();
//				parent.client.cinfo.choices[parent.gambles[parent.index]
//						.getIndex()][parent.client.cinfo.getStatus() / 2 + 4] = (int) (temp
//						.getTime() - parent.now.getTime());
				parent.now = temp;

				// record results
//				parent.client.cinfo.choices[parent.gambles[parent.index].getIndex()]
//				                           [parent.client.cinfo.getStatus() / 2 + 1] 
//				                            	= parent.pages[parent.index].getResults();

				// scrolls
				parent.next();

			}
		});
//
//		// creates bottom area for submit button
		bottom = new JPanel();
		bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
		bottom.setBackground(Color.white);
		bottom.setOpaque(true);
		bottom.add(next);
		bottom.add(Box.createHorizontalGlue());

		JLabel buttonInfo = new JLabel(
				"  <--Clicking this button will submit your decision. Be sure this answer is the one you want before clicking it! You can't go back!   ");
		bottom.add(buttonInfo);

		mainPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
		decisionPanel.add(bottom);
		decisionPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
		constraints.weightx = 1;
		constraints.weighty = 1;
		layout.setConstraints(decisionPanel, constraints);
		centerPanel.add(decisionPanel);
		mainPanel.add(top, BorderLayout.NORTH);
		mainPanel.add(centerPanel, BorderLayout.CENTER);
		now = new Date();
		time = now.getTime();
	}

	// turns to the next page
	public void next() {
		if((boolean) ((RoundPane) gamblePanel.getComponent(0)).goodValues)
		{	
			next.setEnabled(false);
			
			client.data.lastrate = client.data.ratio;
			client.data.lastx = client.data.x;
			client.data.timeToDecide = (now.getTime() - time)/1000;
			client.data.ID = client.cinfo.subjectNumber;
	
			String text = "<html><center>INDEX = "+ client.data.index +" Thank you for placing your pledge!</center></html>";
			body2.setText(text);
			gamblePanel.removeAll();
	
			// send data and our client status
			client.cinfo.setStatus(Pledge.STAGE1_DONE);
			SendData sd = new SendData(client.out, client.cinfo.pledge);
			sd.start();
	
			round++;
			gamblePanel.validate();
			gamblePanel.repaint();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Remember to press Enter.", "ERROR",
				    JOptionPane.ERROR_MESSAGE);
		}
	}
//
//	// goes to last page(messages)
//	public void last() {
//
//		// removes current page
//		gamblePanel.remove(pages[index]);
//
//		// adds messages
//		gamblePanel.add(messages, BorderLayout.CENTER);
//
//		//body1.setText(" ");
//		body2.setText(" ");
//		index = gambles.length;
//		gamblePanel.validate();
//		gamblePanel.repaint();
//	}
//
//	// finds the next page, returns -1 if there is none
//	public int getNext() {
//
//		// finds next page
//		// stage1
//		if (client.cinfo.getStatus() == Gamble.STAGE1_WORKING
//				&& index + 1 < gambles.length) {
//			return index + 1;
//		} else {
//			// if problem, so indicate
//			return -1;
//		}

}
